Welcome to 18.S191 – Fall 2020!
Introduction to Computational Thinking for Real-World Problems
Alan Edelman, David P. Sanders, Grant Sanderson, James Schloss
& Philip the Corgi
Class outline
Data and computation
Module 1: Analyzing images
Module 2: Particles and ray tracing
Module 3: Epidemic spread
Module 4: Climate change
Tools
Julia programming language: http://www.julialang.org/learning
Pluto notebook environment
Module 1: Images
8xxxxxxxxxx4 + 4Data takes many forms
Time series:
Number of infections per day
Stock price each minute
A piece for violin broadcast over the radio
Video:
The view from a window of a self-driving car
A hurricane monitoring station
Images:
Diseased versus healthy tissue in a scan
Deep space via the Hubble telescope
Can your social media account recognise your friends?
Capture your own image!
xxxxxxxxxx raw_camera_data camera_input(;max_size=2000)UndefVarError: grant not defined
- top-level scope@Local: 1
xxxxxxxxxx[ grant grant[:,end:-1:1] grant[end:-1:1,:] grant[end:-1:1,end:-1:1]]xxxxxxxxxxgrant = decimate(process_raw_camera_data(raw_camera_data), 2)What is an image?
Albrecht Dürer:

An image is:
A 2D representation of a 3D world
An approximation
What is an image, though?
A grid of coloured squares called pixels
A colour for each pair
of indicesA discretization
How can we store an image in the computer?
Is it a 1D array (
Vector)?A 2D array (
Matrix)?A 3D array (
tensor)?
If in doubt: Ask Julia!
Let's use the
Images.jlpackage to load an image and see what we get
xxxxxxxxxxbegin import Pkg Pkg.activate(mktempdir())endxxxxxxxxxxbegin Pkg.add(["Images", "ImageIO", "ImageMagick"]) using Imagesend"https://i.imgur.com/VGPeJ6s.jpg"xxxxxxxxxx# defines a variable called `url`# whose value is a string (written inside `"`):url = "https://i.imgur.com/VGPeJ6s.jpg" "philip.jpg"xxxxxxxxxxphilip_file = download(url, "philip.jpg") # download to a local filexxxxxxxxxxphilip = load(philip_file)xxxxxxxxxxphilipArray{RGB{Normed{UInt8,8}},2}xxxxxxxxxxtypeof(philip)xxxxxxxxxxRGBX(0.9, 0.1, 0.1)Enter cell code...
xxxxxxxxxxArray{RGB{Normed{UInt8,8}},2}xxxxxxxxxxtypeof(philip)According to Julia / Pluto, the variable
philipis an imageJulia always returns output
The output can be displayed in a "rich" way
Arthur C. Clarke:
Any sufficiently advanced technology is indistinguishable from magic.
3675
2988
xxxxxxxxxxsize(philip)xxxxxxxxxxphilipxxxxxxxxxxphilip[1:1000, 1:400]How big is Philip?
He's pretty big:
3675
2988
xxxxxxxxxxsize(philip) # Pixel indices walk down a column then move over (right) a row, just like matlabWhich number is which?
xxxxxxxxxxphilipSo, what is an image?
Array{RGB{Normed{UInt8,8}},2}xxxxxxxxxxtypeof(philip)It's an
ArrayThe
2means that it has 2 dimensions (a matrix)
RGBX{Normed{UInt8,8}}is the type of object stored in the arrayA Julia object representing a colour
RGB = Red, Green, Blue
Getting pieces of an image
xxxxxxxxxxbegin (h, w) = size(philip) head = philip[(h ÷ 2):h, (w ÷ 10): (9w ÷ 10)] # `÷` is typed as \div <TAB> -- integer divisionend1839
2392
xxxxxxxxxxsize(head)3675
2988
xxxxxxxxxxsize(philip)Manipulating matrices
An image is just a matrix, so we can manipulate matrices to manipulate the image
xxxxxxxxxx[head head]xxxxxxxxxx[ head reverse(head, dims=2) reverse(head, dims=1) reverse(reverse(head, dims=1), dims=2)]Manipulating an image
How can we get inside the image and change it?
There are two possibilities:
Modify (mutate) numbers inside the array – useful to change a small piece
Create a new copy of the array – useful to alter everything together
Painting a piece of an image
Let's paint a corner red
We'll copy the image first so we don't destroy the original
xxxxxxxxxxnew_phil = copy(head)xxxxxxxxxxred = RGB(1, 0, 0)xxxxxxxxxxfor i in 1:100 for j in 1:300 new_phil[i, j] = red endendNote that for loops do not return anything (or, rather, they return nothing)
xxxxxxxxxxnew_philElement-wise operations: "Broadcasting"
Julia provides powerful technology for operating element by element: broadcasting
Adding "
." applies an operation element by element
xxxxxxxxxxbegin new_phil2 = copy(new_phil) new_phil2[100:200, 1:100] .= RGB(0, 1, 0) new_phil2endxxxxxxxxxxnew_phil2Modifying the whole image at once
We can use the same trick to modify the whole image at once
Let's redify the image
We define a function that turns a colour into just its red component
redify (generic function with 1 method)xxxxxxxxxxfunction redify(c) return RGB(c.r, 0, 0)endxxxxxxxxxxbegin color = RGB(0.9, 0.7, 0.2) [color, redify(color)]endxxxxxxxxxxredify.(philip)xxxxxxxxxxGray.(philip)Transforming an image
The main goal of this week will be to transfrom images in more interesting ways
First let's decimate poor Phil
xxxxxxxxxxpoor_phil = decimate(head, 5)Experiments come alive with interaction
We start to get a feel for things when we can experiment!
xxxxxxxxxxbegin Pkg.add("PlutoUI") using PlutoUIendxxxxxxxxxx repeat_count Slider(1:10, show_value=true)xxxxxxxxxxrepeat(poor_phil, repeat_count, repeat_count)Summary
Images are readily-accessible data about the world
We want to process them to extract information
Relatively simple mathematical operations can transform images in useful ways
expand (generic function with 2 methods)extract_red (generic function with 1 method)decimate (generic function with 2 methods)Appendix
Package environment
Camera input
camera_input (generic function with 1 method)process_raw_camera_data (generic function with 1 method)